package com.fit.pages;

import java.util.Collection;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.beans.BuyBean;
import com.fit.interfaces.BuldingMaterials;
import com.fit.interfaces.Buy;
import com.fit.interfaces.Buyer;
import com.fit.interfaces.User;
import com.fit.interfaces.Worker;
import com.fit.service.BuldingsMaterials;
import com.fit.service.Buyers;
import com.fit.service.Buys;
import com.fit.service.Workers;

public class EntryBuy {

	@SessionState
	private User user;

	private boolean userExists;
	
	public User getUser() {
		return user;
	}
	
	Object onActivate() {
		if (!userExists) return Login.class;
		return null;
	}
	@Persist
	private String buyBuyer;
	@Persist
	private String buyWorker;
	@Persist
	private String buyBuldingMaterials;
	
	private ViewBuy viewBuyPage;
	@InjectPage
	private ViewError viewErrorPage;

	@Inject
	private BuldingsMaterials buldingsMaterials;
	@Inject
	private Buyers buyers;
	@Inject
	private Workers workers;
	@Inject
	private Buys buys;
	
	public Collection<BuldingMaterials> getBuldingsMaterials() {
		return buldingsMaterials.retrieve();
	}

	public Collection<Buyer> getBuyers() {
		return buyers.retrieve();
	}

	public Collection<Worker> getWorkers() {
		return workers.retrieve();
	}

	@OnEvent(value = "submit", component = "buyInputForm")
	Object onFromSubmit() {

		BuldingMaterials buldingMaterials = null;
		Buyer buyer = null;
		Worker worker= null;

		try {
			buyer = buyers.retrieveSingleBuyer(getBuyBuyer());
			worker =workers
					.retrieveSingleWorker(getBuyWorker());
			buldingMaterials= buldingsMaterials.retrieveSingleBuldingMaterials(getBuyBuldingMaterials());
		} catch (Exception ex) {
			viewErrorPage.setPassedMessage(ex.toString());
			return viewErrorPage;
		}
		
		if (buyer == null) {
			viewErrorPage
					.setPassedMessage("Greska u prepoznavanju objekta Buyer u Map-i");
			return viewErrorPage;
		}
		if (worker == null) {
			viewErrorPage
					.setPassedMessage("Greska u prepoznavanju objekta Worker-a u Map-i");
			return viewErrorPage;
		}
		if (buldingMaterials == null) {
			viewErrorPage
					.setPassedMessage("Greska u prepoznavanju objekta BuldingsMaterials u Map-i");
			return viewErrorPage;
		}

		Buy buyBean = new BuyBean();
		buyBean.executeBuyCreation(worker, buyer, buldingMaterials, buys);

		this.buyWorker= "";
		this.buyBuyer = "";
		this.buyBuldingMaterials = "";
		
		return viewBuyPage;
	}

	public String getBuyBuyer() {
		return buyBuyer;
	}

	public void setBuyBuyer(String buyBuyer) {
		this.buyBuyer = buyBuyer;
	}

	public String getBuyWorker() {
		return buyWorker;
	}

	public void setBuyWorker(String buyWorker) {
		this.buyWorker = buyWorker;
	}

	public String getBuyBuldingMaterials() {
		return buyBuldingMaterials;
	}

	public void setBuyBuldingMaterials(String buyBuldingMaterials) {
		this.buyBuldingMaterials = buyBuldingMaterials;
	}
	

}
